package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	public final static String encode(String s) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] b = md5.digest(s.getBytes());
			StringBuilder md5str = new StringBuilder();
			for (int i = 0; i < b.length; i++) {
				String tmp = (Integer.toHexString(b[i] & 0xFF));
				if (tmp.length() == 1) {
					md5str.append("0");
				}
				md5str.append(tmp);
			}

			return md5str.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String [] args){
		System.out.println(MD5.encode("YangChen"));
	}
}
